<?php
namespace modules\auth;
use \modules\ui;
use \phiction\array_utils as arr;

class page
{
    static function mapping()
    {
        return [
            '/login' => 'login',
            '/denied' => 'denied',
        ];
    }

    static function login($q, $html, &$args)
    {
        $redirect_refresh = arr::get_or($args, 'REDIRECT-REFRESH', false);
        $redirect = $redirect_refresh ? null : '/';

        $message = null;
        if (arr::get($args, 'MESSAGE', $msg))
            $message = ['div', 'class' => 'error-message', $msg];

        $form = ui::form($html, '/api/auth/login',
            ui::icon_label('登录', 'sign-in'), [
                ['login', 'text', '用户名'],
                ['password', 'password', '密码'],
            ], [
                [ui::icon_label('取消', 'angle-left'), '/', 'class' => 'go-back'],
            ], $redirect);

        $args['SUBTITLE'] = '登录';
        return ['.',
            $message,
            $form,
        ];
    }

    static function denied($q, $html, &$args)
    {
        $message = arr::get_or($args, 'MESSAGE', '权限不足') . "：请尝试使用有权限的账号登录";
        $args['MESSAGE'] = $message;
        $args['REDIRECT-REFRESH'] = true;
        return self::login($q, $html, $args);
    }
}

